package com.jims.master.modules.reg.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by admin on 2017/3/3.
 */
public class DateUtil {

    public static List getDates(String dateFrom, String dateEnd, List weekDays) {
        long time;
        long perDayMilSec = 24L * 60 * 60 * 1000;
        List dateList = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
            dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
            while (true) {
                time = sdf.parse(dateFrom).getTime();
                time = time + perDayMilSec;
                Date date = new Date(time);
                dateFrom = sdf.format(date);
                if (dateFrom.compareTo(dateEnd) <= 0) {
// 查询的某一时间的星期系数
                    Integer weekDay = dayForWeek(date);
// 判断当期日期的星期系数是否是需要查询的
                    if (strWeekNumber.contains(weekDay.toString())) {
                        dateList.add(dateFrom);
                    }
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * 得到对应星期的系数 0：1，星期
     */
    public static String weekForNum(List<Integer> weekDays) {
// 返回结果为组合的星期系数
        String weekNumber = "";


        for (Integer weekDay : weekDays) {
            weekNumber = weekNumber + "" + getWeekNum(weekDay).toString();
        }
        return weekNumber;


    }

    // 将星期转换为对应的系数 0,星期日; 1,星期一; 2....
    public static Object getWeekNum(int strWeek) {
        return weekNumberMap.get(strWeek);
    }

    /**
     * 输入一个日期的时间段，以及相应的星期数，获得这些星期的日期
     */
    private static Map weekNumberMap = new HashMap();

    static {
        weekNumberMap.put(0, 1);
        weekNumberMap.put(1, 2);
        weekNumberMap.put(2, 3);
        weekNumberMap.put(3, 4);
        weekNumberMap.put(4, 5);
        weekNumberMap.put(5, 6);
        weekNumberMap.put(6, 7);
    }

    // 等到当期时间的周系数。星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取几天后的这个时间
     *
     * @param beginDate
     * @param daysAfter
     * @return
     */
    public static Date getDaysAfter(Date beginDate, int daysAfter) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginDate);
        calendar.add(Calendar.DAY_OF_MONTH, daysAfter);
        return calendar.getTime();
    }

    /**
     * 小时差
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int hourBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws java.text.ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

}
